﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataModel;
using SchoolBusiness;

namespace SchoolAdministration
{
    public partial class AddStudents : System.Web.UI.Page
    {
       private readonly Student st=new Student();
       private int imageId = 0;
         private readonly BLImages _blImage = new BLImages();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateDateofBirth();
            }
        }
        private void PopulateDateofBirth()
        {
            for (int i = 1970; i <= 2020; i++)
            {
                ddlYear.Items.Add(i.ToString());
            }
            ddlYear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected = true;

            DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);

            for (int i = 1; i < 13; i++)
            {
                ddlMonth.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));

            }
            FillDays();
            }

        public void FillDays()
        {
            ddlDays.Items.Clear();
            int noofdays = DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue));
            for (int i = 1; i <= noofdays; i++)
            {
                ddlDays.Items.Add(i.ToString());
            }
            ddlDays.Items.FindByValue(System.DateTime.Now.Day.ToString()).Selected = true;
        }

        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDays();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Images img = new Images();
            if (fupPhoto.HasFile)
            {
                try
                {
                    string[] allowdFile = { ".png", ".jpeg", ".jpg", ".gif", ".bmp" };
                    string FileExt = System.IO.Path.GetExtension(fupPhoto.PostedFile.FileName);
                    bool isValidFile = allowdFile.Contains(FileExt);
                    if (!isValidFile)
                    {
                        lblerror.ForeColor = System.Drawing.Color.Red;
                        lblerror.Text = "Please image only ";
                    }
                    else
                    {
                        if (fupPhoto.PostedFile.ContentLength <= 602400)
                        {
                            img.PhotoName = fupPhoto.FileName;
                            string path = Server.MapPath("StudentPhoto/" + Guid.NewGuid() + img.PhotoName);
                            fupPhoto.SaveAs(path);
                            int getPos = path.LastIndexOf("\\");
                            int len = path.Length;
                            string getPath = path.Substring(getPos, len - getPos);
                            string pathToStore = getPath.Remove(0, 1);
                            img.PhotoPath = "~/StudentPhoto/" + pathToStore;
                            imageId = _blImage.SaveImage(img);
                            MStudent student = new MStudent()
                            {
                                FirstName = TxtStudentFName.Text,
                                LastName = TxtStudentLName.Text,
                                MiddleName = TxtMiddleName.Text,
                                Roll = Convert.ToInt32(TxtRoll.Text),
                                Address = StudentAddress.Text,
                                DOB = Convert.ToDateTime(ddlYear.Text + "-" + ddlMonth.SelectedValue.ToString() + "-" + ddlDays.Text),
                                Grade = Convert.ToInt32(Grade.Text),
                                section = TxtSection.Text,
                                Hostel = ChkHostel.Checked,
                                Transport = ChkTransport.Checked,
                                Mobile = StuMobile.Text,
                                Phone = txtstPhone.Text,
                                photoId = imageId,
                                Parent = new StudentParent()
                                {
                                    FirstName = ParentFName.Text,
                                    MiddleName = ParentMName.Text,
                                    LastName = ParentLName.Text,
                                    Address = ParentAddress.Text,
                                    Phone = ParentPhone.Text
                                },
                            };

                            int sresult = st.addstudent(student);

                            if (sresult > 1)
                            {
                                lblSucess.Text = "Successfully entered Student";
                                ClearAll();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    lblerror.Text = "Error occurred while uploading a file";
                }
            }
           

        }
        public void ClearAll()
        {

            TxtStudentFName.Text = "";
            TxtMiddleName.Text = "";
            TxtStudentLName.Text = "";
            TxtRoll.Text = "";
            TxtSection.Text = "";
            Grade.Text = "";
            ParentFName.Text = "";
            ParentLName.Text = "";
            ParentMName.Text = "";
            ParentMobile.Text = "";
            ParentPhone.Text = "";
            ParentAddress.Text = "";
        }

        protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDays();
        }

    }
}